package com.ruoyi.clinic.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.clinic.domain.ClinicCity;
import com.ruoyi.clinic.domain.ClinicDistrict;
import com.ruoyi.clinic.domain.ClinicProvince;
import com.ruoyi.clinic.dto.*;
import com.ruoyi.clinic.mapper.ClinicCityMapper;
import com.ruoyi.clinic.mapper.ClinicDistrictMapper;
import com.ruoyi.clinic.mapper.ClinicProvinceMapper;
import com.ruoyi.clinic.service.IClinicProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 省市区信息Service业务层处理
 *
 * @author Tommy
 * @date 2025-11-08
 */
@Service
public class ClinicProvinceServiceImpl implements IClinicProvinceService {

    @Autowired
    private ClinicProvinceMapper provinceMapper;

    @Autowired
    private ClinicCityMapper cityMapper;

    @Autowired
    private ClinicDistrictMapper districtMapper;

    @Override
    public List<ClinicProvinceResDTO> queryClinicProvinceCityList() {
        // 查询省和市
        List<ClinicProvince> clinicProvinces = provinceMapper.selectProvinceList();
        List<ClinicProvinceResDTO> clinicProvinceResDTOs = BeanUtil.copyToList(clinicProvinces, ClinicProvinceResDTO.class);
        if (CollectionUtil.isEmpty(clinicProvinceResDTOs)) {
            return null;
        }
        List<Long> provinceIdList = clinicProvinceResDTOs.stream().map(ClinicProvinceResDTO::getId).toList();
        List<ClinicCity> clinicCities = cityMapper.selectCityListByProvinceIds(provinceIdList);
        List<ClinicCityResDTO> clinicCityVos = BeanUtil.copyToList(clinicCities, ClinicCityResDTO.class);
        if (CollectionUtil.isEmpty(clinicCityVos)) {
            return clinicProvinceResDTOs;
        }
        Map<Long, List<ClinicCityResDTO>> clinicCityResDTOListMap = clinicCityVos.stream().collect(Collectors.groupingBy(ClinicCityResDTO::getProvinceId));
        clinicProvinceResDTOs.forEach(one -> one.setCityList(clinicCityResDTOListMap.get(one.getId())));
        return clinicProvinceResDTOs;
    }

    @Override
    public List<ClinicProvinceResDTO> queryClinicProvinceCityDistrictList() {
        // 查询省和市和区
        List<ClinicProvince> clinicProvinces = provinceMapper.selectProvinceList();
        List<ClinicProvinceResDTO> clinicProvinceResDTOs = BeanUtil.copyToList(clinicProvinces, ClinicProvinceResDTO.class);
        if (CollectionUtil.isEmpty(clinicProvinceResDTOs)) {
            return null;
        }
        List<Long> provinceIdList = clinicProvinceResDTOs.stream().map(ClinicProvinceResDTO::getId).toList();
        // 查询城市
        List<ClinicCity> clinicCities = cityMapper.selectCityListByProvinceIds(provinceIdList);
        List<ClinicCityResDTO> clinicCityResDTOs = BeanUtil.copyToList(clinicCities, ClinicCityResDTO.class);
        if (CollectionUtil.isEmpty(clinicCityResDTOs)) {
            return clinicProvinceResDTOs;
        }
        List<Long> cityIdList = clinicCityResDTOs.stream().map(ClinicCityResDTO::getId).toList();
        Map<Long, List<ClinicCityResDTO>> ClinicCityResDTOListMap = clinicCityResDTOs.stream().collect(Collectors.groupingBy(ClinicCityResDTO::getProvinceId));
        clinicProvinceResDTOs.forEach(one -> one.setCityList(ClinicCityResDTOListMap.get(one.getId())));
        // 查询区县
        List<ClinicDistrict> clinicDistricts = districtMapper.selectDistrictListByCityIds(cityIdList);
        List<ClinicDistrictResDTO> clinicDistrictResDTOs = BeanUtil.copyToList(clinicDistricts, ClinicDistrictResDTO.class);
        if (CollectionUtil.isEmpty(clinicDistrictResDTOs)) {
            return clinicProvinceResDTOs;
        }
        Map<Long, List<ClinicDistrictResDTO>> ClinicDistrictResDTOListMap = clinicDistrictResDTOs.stream().collect(Collectors.groupingBy(ClinicDistrictResDTO::getCityId));
        clinicCityResDTOs.forEach(one -> one.setDistrictList(ClinicDistrictResDTOListMap.get(one.getId())));
        return clinicProvinceResDTOs;
    }

    @Override
    public List<ClinicDistrictResDTO> queryDistrictListByCityId(ClinicDistrictReqDTO reqDTO) {
        if (null == reqDTO || null == reqDTO.getCityId()) {
            return null;
        }
        List<ClinicDistrict> clinicDistricts = districtMapper.selectDistrictListByCityIds(Collections.singletonList(reqDTO.getCityId()));
        return BeanUtil.copyToList(clinicDistricts, ClinicDistrictResDTO.class);
    }
}
